package com.example.ssm.controller;

import com.example.ssm.enums.PaymentType;
import com.example.ssm.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付演示控制器
 * 用于演示策略模式结合枚举的使用
 */
@Controller
@RequestMapping("/demo/payment")
public class PaymentDemoController {

    @Autowired
    private PaymentService paymentService;

    /**
     * 演示所有支付方式
     * @return 所有支付方式的处理结果
     */
    @GetMapping("/demo")
    @ResponseBody
    public Map<String, Object> demo() {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> paymentResults = new ArrayList<>();

        // 模拟订单信息
        String orderId = "ORDER" + System.currentTimeMillis();
        BigDecimal amount = new BigDecimal("100.50");

        // 使用字符串代码方式调用
        Map<String, Object> alipayResult = new HashMap<>();
        alipayResult.put("type", "使用字符串代码方式");
        alipayResult.put("paymentType", "alipay");
        alipayResult.put("result", paymentService.processPayment("alipay", amount, orderId));
        paymentResults.add(alipayResult);

        Map<String, Object> wechatResult = new HashMap<>();
        wechatResult.put("type", "使用字符串代码方式");
        wechatResult.put("paymentType", "wechat");
        wechatResult.put("result", paymentService.processPayment("wechat", amount, orderId));
        paymentResults.add(wechatResult);

        Map<String, Object> bankCardResult = new HashMap<>();
        bankCardResult.put("type", "使用字符串代码方式");
        bankCardResult.put("paymentType", "bankCard");
        bankCardResult.put("result", paymentService.processPayment("bankCard", amount, orderId));
        paymentResults.add(bankCardResult);

        // 使用枚举方式调用
        Map<String, Object> alipayEnumResult = new HashMap<>();
        alipayEnumResult.put("type", "使用枚举方式");
        alipayEnumResult.put("paymentType", PaymentType.ALIPAY.getDesc());
        alipayEnumResult.put("result", paymentService.processPayment(PaymentType.ALIPAY, amount, orderId));
        paymentResults.add(alipayEnumResult);

        Map<String, Object> wechatEnumResult = new HashMap<>();
        wechatEnumResult.put("type", "使用枚举方式");
        wechatEnumResult.put("paymentType", PaymentType.WECHAT.getDesc());
        wechatEnumResult.put("result", paymentService.processPayment(PaymentType.WECHAT, amount, orderId));
        paymentResults.add(wechatEnumResult);

        Map<String, Object> bankCardEnumResult = new HashMap<>();
        bankCardEnumResult.put("type", "使用枚举方式");
        bankCardEnumResult.put("paymentType", PaymentType.BANK_CARD.getDesc());
        bankCardEnumResult.put("result", paymentService.processPayment(PaymentType.BANK_CARD, amount, orderId));
        paymentResults.add(bankCardEnumResult);

        // 测试不支持的支付方式
        Map<String, Object> unsupportedResult = new HashMap<>();
        unsupportedResult.put("type", "不支持的支付方式");
        unsupportedResult.put("paymentType", "bitcoin");
        unsupportedResult.put("result", paymentService.processPayment("bitcoin", amount, orderId));
        paymentResults.add(unsupportedResult);

        result.put("success", true);
        result.put("message", "支付策略模式演示");
        result.put("results", paymentResults);

        return result;
    }
}
